//
//  SearchTableViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/27.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SearchTableViewController.h"
#import "DataManager.h"
#import "Hospital.h"
#import "NearbyTableViewCell.h"
#import "DetailsTableViewController.h"

#define kShared [DataManager defaultManager]

@interface SearchTableViewController ()<UISearchBarDelegate>
//数据源
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) NSMutableArray *searchArray;

@end

@implementation SearchTableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTableViews];
}
//TableView设置
- (void)addTableViews
{
    //隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    //设置tableView大小
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20,[UIScreen  mainScreen].bounds.size.width ,[UIScreen  mainScreen].bounds.size.height)];
    self.searchBar  = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.tableView.frame.size.width, 44)];
    [self.tableView addSubview:self.searchBar];
    self.searchBar.delegate = self;
    //注册Cell
    //注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"NearbyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NearbyCell"];
    
    self.tableView.tableHeaderView = self.searchBar;
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//懒加载
-(NSMutableArray *)searchArray{
    if (_searchArray == nil) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray
    ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyCell" forIndexPath:indexPath];
    Hospital *hospital = self.searchArray[indexPath.row];
    cell.hospitalLabel.text = hospital.hospitalName;
    cell.numberLabel.text = [NSString stringWithFormat:@"%@ 位医生",hospital.doctorCount];
    cell.levelLabel.text = hospital.hospitalLevel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"搜索结果";
}

//搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if(self.searchArray.count > 0)
    {
        [self.searchArray removeAllObjects];
    }
    NSString *str = searchBar.text;
    for(Hospital *hospital in kShared.hospitalArray)
    {
        if([hospital.hospitalName containsString:str])
        {
            [self.searchArray addObject:hospital];
        }
    }
    //刷新表格
    [self.tableView reloadData];
    [searchBar resignFirstResponder];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsTableViewController *details = [[DetailsTableViewController alloc] init];
    details.hospital = self.searchArray[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
